package com.lyj.hw_student.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyj.hw_student.system.entity.SysRole;
import com.lyj.hw_student.system.entity.SysUser;
import com.lyj.hw_student.system.entity.SysUserRole;
import com.lyj.hw_student.system.mapper.SysRoleMapper;
import com.lyj.hw_student.system.service.ISysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyj.hw_student.system.service.ISysUserRoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lyj
 * @since 2025-04-22
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    ISysUserRoleService sysUserRoleService;
    //在实现类实现service接口中定义的抽象方法
    @Override
    public void testRoleAdd(SysRole sysRole) {
        //将新增的角色保存到角色表中
        this.save(sysRole);
        //将新增的角色保存到用户角色表中
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setRoleId(sysRole.getRoleId());
        sysUserRole.setUserId(1);
        sysUserRoleService.save(sysUserRole);

    }

    /**
     * 分页查询角色列表
     * @param page
     * @param roleName
     * @return
     */
    @Override
    public Page<SysRole> queryRolePage(Page<SysRole> page,String roleName) {
        //构造查询条件（创建一个查询条件的实例）
        LambdaQueryWrapper<SysRole> querywrapper = new LambdaQueryWrapper<SysRole>();
        if (StringUtils.isNotBlank(roleName)){
            querywrapper.like(SysRole::getRoleName,roleName);
        }

        return this.page(page, querywrapper);
    }
}
